package com.grandtech.insurance.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.grandtech.insurance.common.GenerateRandomUUID;
import com.grandtech.insurance.common.HttpClient;
import com.grandtech.insurance.common.MD5Utils;
import com.grandtech.insurance.dao.*;
import com.grandtech.insurance.domain.*;
import com.grandtech.insurance.feign.Nx2ServerBaseService;
import com.grandtech.insurance.feign.NxServerBaseService;
import com.grandtech.insurance.service.DbKhxxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Transactional(rollbackFor={Exception.class})
@Service
public class DbKhxxServiceImpl implements DbKhxxService {

    @Autowired
    DbKhxxDAO dbKhxxDAO;
    @Autowired
    DbJtcyxxDAO dbJtcyxxDAO;
    @Autowired
    DbZzxxDAO dbZzxxDAO;
    @Autowired
    DbYzxxDAO dbYzxxDAO;
    @Autowired
    DbDhlzxxDAO dbDhlzxxDAO;
    @Autowired
    DbZpsxDAO dbZpsxDAO;
    @Autowired
    SysAdminareaDAO sysAdminareaDAO;
    @Autowired
    DbKhdkxxDAO dbKhdkxxDAO;
    @Autowired
    DbKhyzmxDAO dbKhyzmxDAO;
    @Autowired
    Nx2ServerBaseService nx2ServerBaseService;

    @Override
    public int updateByPrimaryKeySelective(DbKhxx record) {
        return dbKhxxDAO.updateByPrimaryKeySelective(record);
    }

    @Override
    public CjxxAll getCjxxAll(DbKhxx param) {
        CjxxAll cjxxAll = new CjxxAll();

        DbKhxx dbKhxx = dbKhxxDAO.getDbKhxxByIdcodeAndXmbm(param);
        if(dbKhxx!=null){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(dbKhxx.getCreateTime()!=null){
                String createTime1 = formatter.format(dbKhxx.getCreateTime());
                dbKhxx.setCreateTime1(createTime1);
            }
            if(dbKhxx.getSbTime()!=null){
                String sbTime1 = formatter.format(dbKhxx.getSbTime());
                dbKhxx.setSbTime1(sbTime1);
            }

        }

        if(dbKhxx!=null){
            List<DbJtcyxx> jtcyxxList = dbJtcyxxDAO.getDbJtcyxxByIdcodeAndXmbm(param);
            List<DbZzxx> zzxxList = dbZzxxDAO.getDbZzxxByIdcodeAndXmbm(param);
            List<DbYzxx> yzxxList = dbYzxxDAO.getDbYzxxByIdcodeAndXmbm(param);
            List<DbDhlzxx> dhlzxxList = dbDhlzxxDAO.getDbDhlzxxByIdcodeAndXmbm(param);
            List<DbZpsx> zpsxList = dbZpsxDAO.getDbZpsxByIdcodeAndXmbm(param);
            cjxxAll.setDbKhxx(dbKhxx);
            cjxxAll.setJtcyxxList(jtcyxxList);
            cjxxAll.setZzxxList(zzxxList);
            cjxxAll.setYzxxList(yzxxList);
            cjxxAll.setDhlzxxList(dhlzxxList);
            cjxxAll.setZpsxList(zpsxList);
        }

        return cjxxAll;
    }

    @Override
    public CjxxAll1 getCjxxAll1(DbKhxx param) {
        CjxxAll1 cjxxAll = new CjxxAll1();
        DbKhxx dbKhxx = dbKhxxDAO.getDbKhxxByIdcodeAndXmbm(param);
        if(dbKhxx!=null){
            param.setIfMarried(dbKhxx.getIfMarried());
        }
        if(dbKhxx!=null){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(dbKhxx.getCreateTime()!=null){
                String createTime1 = formatter.format(dbKhxx.getCreateTime());
                dbKhxx.setCreateTime1(createTime1);
            }
            if(dbKhxx.getSbTime()!=null){
                String sbTime1 = formatter.format(dbKhxx.getSbTime());
                dbKhxx.setSbTime1(sbTime1);
            }

        }
        if(dbKhxx!=null){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(dbKhxx.getCreateTime()!=null){
                String createTime1 = formatter.format(dbKhxx.getCreateTime());
                dbKhxx.setCreateTime1(createTime1);
            }
            if(dbKhxx.getSbTime()!=null){
                String sbTime1 = formatter.format(dbKhxx.getSbTime());
                dbKhxx.setSbTime1(sbTime1);
            }

        }
        if(dbKhxx!=null){
            //家庭
            List<DbJtcyxx> jtcyxxList = dbJtcyxxDAO.getDbJtcyxxByIdcodeAndXmbm(param);
            //获取配偶
            if(jtcyxxList.size()>0){
                for(DbJtcyxx jt : jtcyxxList){
                    if("10".equals(jt.getGxdm())){
                        dbKhxx.setPoname(jt.getGxname());
                        dbKhxx.setPoidcode(jt.getGxidCode());
                    }
                }
            }
            //家庭-异常
            DbKhyzmx param1 = new DbKhyzmx();
            param1.setIdcode(param.getIdcode());
            param1.setXmbm(param.getXmbm());
            param1.setYzbm("1202");
            List<DbKhyzmx> mxlist1 = dbKhyzmxDAO.getDbKhyzmxByTJ(param1);
            if(jtcyxxList.size()>0 && mxlist1.size()>0){
                for(DbJtcyxx jt : jtcyxxList){
                    if(mxlist1.get(0).getYzjgxx()!=null){
                        if(jt.getGxidCode()!=null){
                            if(mxlist1.get(0).getYzjgxx().contains(jt.getGxidCode())){
                                jt.setFlag(1);
                            }
                        }
                    }
                }
            }

            //种植
            List<DbZzxx> zzxxList = dbZzxxDAO.getDbZzxxByIdcodeAndXmbm(param);
            BigDecimal zzmj = new BigDecimal(0);
            if(zzxxList.size()>0){
                for(DbZzxx zzxx : zzxxList){
                    zzmj = zzmj.add(zzxx.getMj());
                }
            }
            //养殖
            List<DbYzxx> yzxxList = dbYzxxDAO.getDbYzxxByIdcodeAndXmbm(param);
            int yzsl = 0;
            if(yzxxList.size()>0){
                for(DbYzxx yzxx : yzxxList){
                    yzsl = yzsl + yzxx.getSl();
                }
            }
            //流转
            List<DbDhlzxx> dhlzxxList = dbDhlzxxDAO.getDbDhlzxxByIdcodeAndXmbm(param);
            int lzhs = 0;
            BigDecimal lzmj = new BigDecimal(0);
            if(dhlzxxList.size()>0){
                for(DbDhlzxx dhlzxx : dhlzxxList){
                    lzhs++;
                    lzmj = lzmj.add(dhlzxx.getLzmj());
                }
            }
            //流转-异常
            DbKhyzmx param2 = new DbKhyzmx();
            param2.setIdcode(param.getIdcode());
            param2.setXmbm(param.getXmbm());
            param2.setYzbm("1204");
            List<DbKhyzmx> mxlist2 = dbKhyzmxDAO.getDbKhyzmxByTJ(param2);
            if(dhlzxxList.size()>0 && mxlist2.size()>0){
                for(DbDhlzxx lz : dhlzxxList){
                    if(mxlist2.get(0).getYzjgxx()!=null){
                        if(mxlist2.get(0).getYzjgxx().contains(lz.getZbfidCode())){
                            lz.setFlag1(1);
                        }
                    }
                }
            }
            DbKhyzmx param3 = new DbKhyzmx();
            param3.setIdcode(param.getIdcode());
            param3.setXmbm(param.getXmbm());
            param3.setYzbm("1205");
            List<DbKhyzmx> mxlist3 = dbKhyzmxDAO.getDbKhyzmxByTJ(param3);
            if(dhlzxxList.size()>0 && mxlist3.size()>0){
                for(DbDhlzxx lz : dhlzxxList){
                    if(mxlist3.get(0).getYzjgxx()!=null){
                        if(mxlist3.get(0).getYzjgxx().contains(lz.getZbfidCode())){
                            lz.setFlag2(1);
                        }
                    }
                    if(mxlist3.get(0).getYzjgms()!=null){
                        if(mxlist3.get(0).getYzjgms().contains("无参考记录")){
                            lz.setFlag2(1);
                        }
                    }
                }
            }
            //地块
            List<DbKhdkxx> khdkxxList = dbKhdkxxDAO.getKhdkxxByIdcodeAndXmbm(param);
            int dbzzdksl = 0;
            BigDecimal dbzzmj = new BigDecimal(0);
            int dbyzdksl = 0;
            BigDecimal dbyzmj = new BigDecimal(0);
            if(khdkxxList.size()>0){
                for(DbKhdkxx khdkxx : khdkxxList){
                    if("01".equals(khdkxx.getZytype())){
                        dbzzdksl++;
                        dbzzmj = dbzzmj.add(khdkxx.getMj());
                    }
                    if("02".equals(khdkxx.getZytype())){
                        dbyzdksl++;
                        dbyzmj = dbyzmj.add(khdkxx.getMj());
                    }
                }
            }
            //图片
            List<DbZpsx> zpsxList = dbZpsxDAO.getDbZpsxByIdcodeAndXmbm(param);
            DbKhyzmx param4 = new DbKhyzmx();
            param4.setIdcode(param.getIdcode());
            param4.setXmbm(param.getXmbm());
            param4.setYzbm("1001");
            List<DbKhyzmx> mxlist4 = dbKhyzmxDAO.getDbKhyzmxByTJ(param4);
            if(zpsxList.size()>0 && mxlist4.size()>0){
                for(DbZpsx zp : zpsxList){
                    if(mxlist4.get(0).getYzjgxx()!=null){
                        if(mxlist4.get(0).getYzjgxx().contains(zp.getZpcode())){
                            zp.setFlag(1);
                        }
                    }
                }
            }

            //验证信息
            List<Dbkhyztjdw> yzList = dbKhyzmxDAO.getDbkhyztjdw(param);

            cjxxAll.setDbKhxx(dbKhxx);
            cjxxAll.setJtcyxxList(jtcyxxList);
            cjxxAll.setZzxxList(zzxxList);
            cjxxAll.setZzmj(zzmj);
            cjxxAll.setYzxxList(yzxxList);
            cjxxAll.setYzsl(yzsl);
            cjxxAll.setDhlzxxList(dhlzxxList);
            cjxxAll.setLzhs(lzhs);
            cjxxAll.setLzmj(lzmj);
            cjxxAll.setKhdkxxList(khdkxxList);
            cjxxAll.setDwList(yzList);
            cjxxAll.setDbzzdksl(dbzzdksl);
            cjxxAll.setDbzzmj(dbzzmj);
            cjxxAll.setDbyzdksl(dbyzdksl);
            cjxxAll.setDbyzmj(dbyzmj);
            cjxxAll.setZpsxList(zpsxList);
        }

        return cjxxAll;
    }



    @Transactional(rollbackFor={Exception.class})
    @Override
    public int saveCjxxAll(CjxxAll param) {

        //校验身份证以及重复数据
        //其他家庭成员校验
        /*String regex = "\\d{15}|\\d{17}[\\dxX]";
        List<DbJtcyxx> jts = param.getJtcyxxList();
        Map<String ,Object> map1 = new HashMap<String ,Object>();
        if(jts.size()>0){
            for(DbJtcyxx jt : jts){
                if(!jt.getGxidCode().matches(regex)){
                    return -4;
                }
                map1.put(jt.getGxidCode(),"");
            }
            if(map1.size()!=jts.size()){
                return -6;
            }
        }*/
        //种植信息校验
        /*List<DbZzxx> zzxxs = param.getZzxxList();
        Map<String ,Object> map2 = new HashMap<String ,Object>();
        if(zzxxs.size()>0){
            for(DbZzxx zzxx : zzxxs){
                map2.put(zzxx.getZzdm(),"");
            }
            if(map2.size()!=zzxxs.size()){
                return -7;
            }
        }*/
        //养殖信息校验
        /*List<DbYzxx> yzxxs = param.getYzxxList();
        Map<String ,Object> map3 = new HashMap<String ,Object>();
        if(yzxxs.size()>0){
            for(DbYzxx yzxx : yzxxs){
                map3.put(yzxx.getYzdm(),"");
            }
            if(map3.size()!=yzxxs.size()){
                return -8;
            }
        }*/
        //流转信息
        /*List<DbDhlzxx> lzs = param.getDhlzxxList();
        Map<String ,Object> map4 = new HashMap<String ,Object>();
        if(lzs.size()>0){
            for(DbDhlzxx lz : lzs){
                if(!lz.getZbfidCode().matches(regex)){
                    return -5;
                }
                map4.put(lz.getZbfidCode(),"");
            }
            if(map4.size()!=lzs.size()){
                return -9;
            }
        }*/


        //客户信息
        DbKhxx dbKhxx = param.getDbKhxx();
        //String year = "";
        if(dbKhxx!=null){
            //校验客户信息
            if(dbKhxx.getSbTime()==null){
                return -10;
            }
            if(dbKhxx.getNf()==null || "".equals(dbKhxx.getNf())){
                return -11;
            }
            /*if(dbKhxx.getSbTime()!=null && !"".equals(dbKhxx.getSbTime())){
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
                year = formatter.format(dbKhxx.getSbTime());
            }*/

            //删除
            if(dbKhxx.getIdcode()!=null && dbKhxx.getXmbm()!=null){
                int a = dbKhxxDAO.deleteDbKhxxByIdcodeAndXmbm(dbKhxx);
                int b = dbJtcyxxDAO.deleteDbJtcyxxByIdcodeAndXmbm(dbKhxx);
                int c = dbZzxxDAO.deleteDbZzxxByIdcodeAndXmbm(dbKhxx);
                int d = dbYzxxDAO.deleteDbYzxxByIdcodeAndXmbm(dbKhxx);
                int e = dbDhlzxxDAO.deleteDbDhlzxxByIdcodeAndXmbm(dbKhxx);
                int f = dbZpsxDAO.deleteDbZpsxByIdcodeAndXmbm(dbKhxx);
            }else{
                return -1;
            }
            //区划信息
            /*SysAdminarea sysAdminarea = sysAdminareaDAO.selectByPrimaryKey(dbKhxx.getQhdm());
            if(sysAdminarea!=null){
                dbKhxx.setQhmc(sysAdminarea.getQhmc());
            }*/
            //创建时间
            dbKhxx.setCreateTime(new Date());
            dbKhxx.setYzstate(0);
            dbKhxx.setF(0);
            //dbKhxx.setNf(year);
        }else{
            return -1;
        }

        String zytype = "";
        int a = 0;
        int b = 0;

        //家庭关系
        List<DbJtcyxx> jtcyxxList = new ArrayList<DbJtcyxx>();
        jtcyxxList = param.getJtcyxxList();
        //种植信息
        List<DbZzxx> zzxxList = new ArrayList<DbZzxx>();
        zzxxList = param.getZzxxList();
        if(zzxxList.size()>0){
            a = 1;
        }
        /*if(zzxxList.size()==0){
            return -2;
        }*/
        //养殖信息
        List<DbYzxx> yzxxList = new ArrayList<DbYzxx>();
        yzxxList = param.getYzxxList();
        if(yzxxList.size()>0){
            b = 1;
        }
        //流转信息
        List<DbDhlzxx> dhlzxxList = new ArrayList<DbDhlzxx>();
        dhlzxxList = param.getDhlzxxList();
        /*if(dhlzxxList.size()==0){
            return -3;
        }*/
        //照片信息
        List<DbZpsx> zpsxList = new ArrayList<DbZpsx>();
        zpsxList = param.getZpsxList();

        //保存
        if(a==1 && b==0){
            zytype = "01";
        }
        if(a==0 && b==1){
            zytype = "02";
        }
        if(a==1 && b==1){
            zytype = "03";
        }
        dbKhxx.setZytype(zytype);
        dbKhxx.setId(GenerateRandomUUID.generateRandomUuid());
        //MD5加密
        dbKhxx.setIdcodemd5(MD5Utils.stringToMD5(dbKhxx.getIdcode()));
        int a1 = dbKhxxDAO.insertSelective(dbKhxx);

        DecimalFormat df = new DecimalFormat("0.00");

        if(jtcyxxList.size()>0){
            for(DbJtcyxx jtcyxx : jtcyxxList){
                jtcyxx.setId(GenerateRandomUUID.generateRandomUuid());
                //jtcyxx.setNf(year);
                //jtcyxx.setIdcode(dbKhxx.getIdcode());
                //jtcyxx.setXmbm(dbKhxx.getXmbm());
                //jtcyxx.setNf(dbKhxx.getNf());
                int a2 = dbJtcyxxDAO.insertSelective(jtcyxx);
            }
        }

        if(zzxxList.size()>0){
            for(DbZzxx zzxx : zzxxList){
                zzxx.setId(GenerateRandomUUID.generateRandomUuid());
                zzxx.setMj(new BigDecimal(df.format(zzxx.getMj())));
                //zzxx.setNf(year);
                //zzxx.setIdcode(dbKhxx.getIdcode());
                //zzxx.setXmbm(dbKhxx.getXmbm());
                //zzxx.setNf(dbKhxx.getNf());
                int a3 = dbZzxxDAO.insertSelective(zzxx);
            }
        }

        if(yzxxList.size()>0){
            for(DbYzxx yzxx : yzxxList){
                yzxx.setId(GenerateRandomUUID.generateRandomUuid());
                //yzxx.setNf(year);
                //yzxx.setIdcode(dbKhxx.getIdcode());
                //yzxx.setXmbm(dbKhxx.getXmbm());
                //yzxx.setNf(dbKhxx.getNf());
                int a4 = dbYzxxDAO.insertSelective(yzxx);
            }
        }

        if(dhlzxxList.size()>0){
            for(DbDhlzxx dhlzxx : dhlzxxList){
                dhlzxx.setId(GenerateRandomUUID.generateRandomUuid());
                dhlzxx.setLzmj(new BigDecimal(df.format(dhlzxx.getLzmj())));
                //dhlzxx.setNf(year);
                //dhlzxx.setIdcode(dbKhxx.getIdcode());
                //dhlzxx.setXmbm(dbKhxx.getXmbm());
                //dhlzxx.setNf(dbKhxx.getNf());
                int a5 = dbDhlzxxDAO.insertSelective(dhlzxx);
            }
        }

        if(zpsxList.size()>0){
            for(DbZpsx zpsx : zpsxList){
                //照片编码
                String xh = dbZpsxDAO.getLastZP(zpsx);
                if(xh!=null && xh.length()>=2){
                    //前面的区划+身份证号+年份
                    String q = xh.substring(0,xh.length()-2);
                    //后面序数
                    String h = xh.substring(xh.length()-2);
                    BigDecimal dddd = new BigDecimal(h);
                    BigDecimal eeee = dddd.add(new BigDecimal(1));
                    String hh = eeee+"";
                    if(hh.length()==1){
                        hh = "0"+hh;
                    }
                    zpsx.setZpcode(q+hh);
                }else{
                    if(zpsx.getQhdm().length()>=6 && zpsx.getIdcode().length()>=4 && zpsx.getNf().length()>=2){
                        zpsx.setZpcode(zpsx.getQhdm().substring(0,6)+
                                zpsx.getIdcode().substring(zpsx.getIdcode().length()-4)+
                                zpsx.getNf().substring(zpsx.getNf().length()-2)+
                                "01");
                    }else{
                        //为了处理区划非县级问题
                        if(zpsx.getIdcode().length()>=4 && zpsx.getNf().length()>=2){
                            zpsx.setZpcode(zpsx.getQhdm()+
                                    zpsx.getIdcode().substring(zpsx.getIdcode().length()-4)+
                                    zpsx.getNf().substring(zpsx.getNf().length()-2)+
                                    "01");
                        }
                        //zpsx.setZpcode(null);
                        //zpsx.setZpcode(GenerateRandomUUID.generateRandomUuid());
                    }
                    /*if(dbKhxx.getQhdm().length()>=6 && dbKhxx.getIdcode().length()>=4 && dbKhxx.getNf().length()>=2){
                        zpsx.setZpcode(dbKhxx.getQhdm().substring(0,6)+
                                dbKhxx.getIdcode().substring(dbKhxx.getIdcode().length()-4)+
                                dbKhxx.getNf().substring(dbKhxx.getNf().length()-2)+
                                "01");
                    }else{
                        zpsx.setZpcode("");
                    }*/
                }
                zpsx.setId(GenerateRandomUUID.generateRandomUuid());
                //zpsx.setNf(year);
                //zpsx.setIdcode(dbKhxx.getIdcode());
                //zpsx.setXmbm(dbKhxx.getXmbm());
                //zpsx.setNf(dbKhxx.getNf());
                //zpsx.setQhdm(dbKhxx.getQhdm());
                //zpsx.setQhmc(dbKhxx.getQhmc());
                zpsx.setCreateTime(new Date());
                //区划信息
                /*SysAdminarea sysAdminarea = sysAdminareaDAO.selectByPrimaryKey(zpsx.getQhdm());
                if(sysAdminarea!=null){
                    zpsx.setQhmc(sysAdminarea.getQhmc());
                }*/
                int a6 = dbZpsxDAO.insertSelective(zpsx);
            }
        }

        Map<String,Object> save = new HashMap<String,Object>();
        save.put("list1", JSONArray.toJSON(dbKhxx).toString());
        save.put("list2", JSONArray.toJSON(jtcyxxList).toString());
        save.put("list3", JSONArray.toJSON(zzxxList).toString());
        save.put("list4", JSONArray.toJSON(yzxxList).toString());
        save.put("list5", JSONArray.toJSON(dhlzxxList).toString());
        save.put("list6", JSONArray.toJSON(zpsxList).toString());
        //HttpClient httpClient = new HttpClient();
        //String url = zuul5url+"/"+zuul5CICES+zuul5name1;
        //String url = "http://localhost:8007/agribigdata-server-base-dx/api/v1/nydb/saveNydbCJ";
        //String cs = JSONObject.toJSONString(save);
        //String result = httpClient.postHttpClient(url,cs);
        String result = nx2ServerBaseService.saveNydbCJ(save);

        return 1;
    }

    @Override
    public int saveCjxxAll1(CjxxAll param) {

        //客户信息
        DbKhxx dbKhxx = param.getDbKhxx();
        if(dbKhxx!=null){

            //删除
            if(dbKhxx.getIdcode()!=null && dbKhxx.getXmbm()!=null){
                int a = dbKhxxDAO.deleteDbKhxxByIdcodeAndXmbm(dbKhxx);
                int b = dbJtcyxxDAO.deleteDbJtcyxxByIdcodeAndXmbm(dbKhxx);
                int c = dbZzxxDAO.deleteDbZzxxByIdcodeAndXmbm(dbKhxx);
                int d = dbYzxxDAO.deleteDbYzxxByIdcodeAndXmbm(dbKhxx);
                int e = dbDhlzxxDAO.deleteDbDhlzxxByIdcodeAndXmbm(dbKhxx);
                int f = dbZpsxDAO.deleteDbZpsxByIdcodeAndXmbm(dbKhxx);
            }else{
                return -1;
            }
            //区划信息
            /*SysAdminarea sysAdminarea = sysAdminareaDAO.selectByPrimaryKey(dbKhxx.getQhdm());
            if(sysAdminarea!=null){
                dbKhxx.setQhmc(sysAdminarea.getQhmc());
            }*/
            //创建时间
            dbKhxx.setCreateTime(new Date());
            dbKhxx.setYzstate(0);
            dbKhxx.setF(0);
            //dbKhxx.setNf(year);
        }else{
            return -1;
        }

        String zytype = "";
        int a = 0;
        int b = 0;

        //家庭关系
        List<DbJtcyxx> jtcyxxList = new ArrayList<DbJtcyxx>();
        jtcyxxList = param.getJtcyxxList();
        //种植信息
        List<DbZzxx> zzxxList = new ArrayList<DbZzxx>();
        zzxxList = param.getZzxxList();
        if(zzxxList.size()>0){
            a = 1;
        }
        /*if(zzxxList.size()==0){
            return -2;
        }*/
        //养殖信息
        List<DbYzxx> yzxxList = new ArrayList<DbYzxx>();
        yzxxList = param.getYzxxList();
        if(yzxxList.size()>0){
            b = 1;
        }
        //流转信息
        List<DbDhlzxx> dhlzxxList = new ArrayList<DbDhlzxx>();
        dhlzxxList = param.getDhlzxxList();
        /*if(dhlzxxList.size()==0){
            return -3;
        }*/
        //照片信息
        List<DbZpsx> zpsxList = new ArrayList<DbZpsx>();
        zpsxList = param.getZpsxList();

        //保存
        if(a==1 && b==0){
            zytype = "01";
        }
        if(a==0 && b==1){
            zytype = "02";
        }
        if(a==1 && b==1){
            zytype = "03";
        }
        dbKhxx.setZytype(zytype);
        dbKhxx.setId(GenerateRandomUUID.generateRandomUuid());
        //MD5加密
        dbKhxx.setIdcodemd5(MD5Utils.stringToMD5(dbKhxx.getIdcode()));
        int a1 = dbKhxxDAO.insertSelective(dbKhxx);

        DecimalFormat df = new DecimalFormat("0.00");

        if(jtcyxxList.size()>0){
            for(DbJtcyxx jtcyxx : jtcyxxList){
                jtcyxx.setId(GenerateRandomUUID.generateRandomUuid());
                //jtcyxx.setNf(year);
                //jtcyxx.setIdcode(dbKhxx.getIdcode());
                //jtcyxx.setXmbm(dbKhxx.getXmbm());
                //jtcyxx.setNf(dbKhxx.getNf());
                int a2 = dbJtcyxxDAO.insertSelective(jtcyxx);
            }
        }

        if(zzxxList.size()>0){
            for(DbZzxx zzxx : zzxxList){
                zzxx.setId(GenerateRandomUUID.generateRandomUuid());
                zzxx.setMj(zzxx.getMj());
                //zzxx.setNf(year);
                //zzxx.setIdcode(dbKhxx.getIdcode());
                //zzxx.setXmbm(dbKhxx.getXmbm());
                //zzxx.setNf(dbKhxx.getNf());
                int a3 = dbZzxxDAO.insertSelective(zzxx);
            }
        }

        if(yzxxList.size()>0){
            for(DbYzxx yzxx : yzxxList){
                yzxx.setId(GenerateRandomUUID.generateRandomUuid());
                //yzxx.setNf(year);
                //yzxx.setIdcode(dbKhxx.getIdcode());
                //yzxx.setXmbm(dbKhxx.getXmbm());
                //yzxx.setNf(dbKhxx.getNf());
                int a4 = dbYzxxDAO.insertSelective(yzxx);
            }
        }

        if(dhlzxxList.size()>0){
            for(DbDhlzxx dhlzxx : dhlzxxList){
                dhlzxx.setId(GenerateRandomUUID.generateRandomUuid());
                dhlzxx.setLzmj(dhlzxx.getLzmj());
                //dhlzxx.setNf(year);
                //dhlzxx.setIdcode(dbKhxx.getIdcode());
                //dhlzxx.setXmbm(dbKhxx.getXmbm());
                //dhlzxx.setNf(dbKhxx.getNf());
                int a5 = dbDhlzxxDAO.insertSelective(dhlzxx);
            }
        }

        if(zpsxList.size()>0){
            for(DbZpsx zpsx : zpsxList){
                //照片编码
                String xh = dbZpsxDAO.getLastZP(zpsx);
                if(xh!=null && xh.length()>=2){
                    //前面的区划+身份证号+年份
                    String q = xh.substring(0,xh.length()-2);
                    //后面序数
                    String h = xh.substring(xh.length()-2);
                    BigDecimal dddd = new BigDecimal(h);
                    BigDecimal eeee = dddd.add(new BigDecimal(1));
                    String hh = eeee+"";
                    if(hh.length()==1){
                        hh = "0"+hh;
                    }
                    zpsx.setZpcode(q+hh);
                }else{
                    if(zpsx.getQhdm().length()>=6 && zpsx.getIdcode().length()>=4 && zpsx.getNf().length()>=2){
                        zpsx.setZpcode(zpsx.getQhdm().substring(0,6)+
                                zpsx.getIdcode().substring(zpsx.getIdcode().length()-4)+
                                zpsx.getNf().substring(zpsx.getNf().length()-2)+
                                "01");
                    }else{
                        //为了处理区划非县级问题
                        if(zpsx.getIdcode().length()>=4 && zpsx.getNf().length()>=2){
                            zpsx.setZpcode(zpsx.getQhdm()+
                                    zpsx.getIdcode().substring(zpsx.getIdcode().length()-4)+
                                    zpsx.getNf().substring(zpsx.getNf().length()-2)+
                                    "01");
                        }
                        //zpsx.setZpcode(null);
                        //zpsx.setZpcode(GenerateRandomUUID.generateRandomUuid());
                    }
                    /*if(dbKhxx.getQhdm().length()>=6 && dbKhxx.getIdcode().length()>=4 && dbKhxx.getNf().length()>=2){
                        zpsx.setZpcode(dbKhxx.getQhdm().substring(0,6)+
                                dbKhxx.getIdcode().substring(dbKhxx.getIdcode().length()-4)+
                                dbKhxx.getNf().substring(dbKhxx.getNf().length()-2)+
                                "01");
                    }else{
                        zpsx.setZpcode("");
                    }*/
                }
                zpsx.setId(GenerateRandomUUID.generateRandomUuid());
                //zpsx.setNf(year);
                //zpsx.setIdcode(dbKhxx.getIdcode());
                //zpsx.setXmbm(dbKhxx.getXmbm());
                //zpsx.setNf(dbKhxx.getNf());
                //zpsx.setQhdm(dbKhxx.getQhdm());
                //zpsx.setQhmc(dbKhxx.getQhmc());
                zpsx.setCreateTime(new Date());
                //区划信息
                /*SysAdminarea sysAdminarea = sysAdminareaDAO.selectByPrimaryKey(zpsx.getQhdm());
                if(sysAdminarea!=null){
                    zpsx.setQhmc(sysAdminarea.getQhmc());
                }*/
                int a6 = dbZpsxDAO.insertSelective(zpsx);
            }
        }

        return 1;
    }

    @Override
    public Khdktj getKhdktjByIdcodeAndXmbm(DbKhxx param) {
        return dbKhdkxxDAO.getKhdktjByIdcodeAndXmbm(param);
    }

    @Override
    public List<String> getXmbms(DbKhxx param) {
        return dbKhxxDAO.getXmbms(param);
    }

    @Override
    public DbKhxx getDbKhxxByIdcodeAndXmbm(DbKhxx param) {
        return dbKhxxDAO.getDbKhxxByIdcodeAndXmbm(param);
    }

    @Override
    public DbKhxx getDbKhxxByIdcodemd5AndXmbm(DbKhxx param) {
        return dbKhxxDAO.getDbKhxxByIdcodemd5AndXmbm(param);
    }

    @Override
    public int insertStatus(DbKhstatus param) {
        return dbKhxxDAO.insertStatus(param);
    }

    @Override
    public int updateStatus(DbKhstatus param) {
        return dbKhxxDAO.updateStatus(param);
    }

    @Override
    public List<DbKhstatus> getDbKhstatus(DbKhstatus param) {
        return dbKhxxDAO.getDbKhstatus(param);
    }

    @Override
    public List<DbKhxx> getDbKhxxAll(DbKhxx param) {
        return dbKhxxDAO.huode(param);
    }

    @Override
    public List<DbKhxx> huodebyxmbm(DbKhxx param) {
        return dbKhxxDAO.huodebyxmbm(param);
    }
}

























